﻿CREATE PROCEDURE [dbo].[yaf_poll_stats](
                @PollID INT)
AS
    BEGIN
        SELECT a.PollID,
               b.Question,
               b.Closes,
               a.ChoiceID,
               a.Choice,
               a.Votes,
               Stats = (SELECT 100 * a.Votes / CASE SUM(x.Votes) 
                                                 WHEN 0 THEN 1
                                                 ELSE SUM(x.Votes)
                                               END
                        FROM   yaf_Choice x
                        WHERE  x.PollID = a.PollID)
        FROM   yaf_Choice a,
               yaf_Poll b
        WHERE  b.PollID = a.PollID
        AND b.PollID = @PollID
    END


